﻿//
//  Manyou.Toolkit.ManyouParam.cs:
//  Authors:
//	Bryan Chow (cabir.zhou@gmail.com)
//
//
//  No Trademark License- This license does not grant you rights to use any 
//  contributors' name, logo, or trademarks.
//
//  If you bring a patent claim against any contributor over patents that you
//  claim are infringed by the software, your patent license from such 
//  contributor to the software ends automatically.
//
//  If you distribute any portion of the software, you must retain
//  all copyright, patent, trademark, and attribution notices that are present in the software.
//
//  If you distribute any portion of the software in source code form,
//  you may do so only under this license by including a complete copy of this license
//  with your distribution. If you distribute any portion of the software
//  in compiled or object code form, you may only do so under a license that complies with this license.
//
//  The software is licensed "as-is." You bear the risk of using it. 
//  The contributors give no express warranties, guarantees or conditions.
//  You may have additional consumer rights under your local laws which 
//  this license cannot change. To the extent permitted under your local laws,
//  the contributors exclude the implied warranties of merchantability,
//  fitness for a particular purpose and non-infringement.
//

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace Manyou.Toolkit
{
    /// <summary>
    /// Manyou参数类
    /// </summary>
    public class ManyouParam : IComparable
    {
        private string name;
        private object value;

        /// <summary>
        /// 获取参数名称
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        /// <summary>
        /// 获取参数值
        /// </summary>
        public string EncodedValue
        {
            get
            {
                if (value is Array)
                    return HttpUtility.UrlEncode(ConvertArrayToString(value as Array));
                else
                    return HttpUtility.UrlEncode(value.ToString());
            }
        }

        /// <summary>
        /// 获取参数值
        /// </summary>
        public string Value
        {
            get
            {
                if (value is Array)
                    return ConvertArrayToString(value as Array);
                else
                    return value.ToString();
            }
        }

        /// <summary>
        /// 构造参数
        /// </summary>
        /// <param name="name">参数名称</param>
        /// <param name="value">参数值</param>
        protected ManyouParam(string name, object value)
        {
            this.name = name;
            this.value = value;
        }

        /// <summary>
        /// 生成字符串
        /// </summary>
        /// <returns>返回字符串的名值对</returns>
        public override string ToString()
        {
            return string.Format("{0}={1}", Name, Value);
        }

        public string ToEncodedString()
        {
            return string.Format("{0}={1}", Name, EncodedValue);
        }

        /// <summary>
        /// 创建Manyou参数
        /// </summary>
        /// <param name="name">参数名称</param>
        /// <param name="value">参数值</param>
        /// <returns>返回Manyou参数</returns>
        public static ManyouParam Create(string name, object value)
        {
            return new ManyouParam(name, value);
        }

        /// <summary>
        /// 比较参数是否相同
        /// </summary>
        /// <param name="obj">要同当前参数比较的参数</param>
        /// <returns>0相同,非0则不同</returns>
        public int CompareTo(object obj)
        {
            if (!(obj is ManyouParam))
                return -1;

            return this.name.CompareTo((obj as ManyouParam).name);
        }

        /// <summary>
        /// 将Manyou参数数组转换为名值串
        /// </summary>
        /// <param name="a">Manyou参数数组</param>
        /// <returns>转换的名值串,名值串之间用逗号分隔</returns>
        private static string ConvertArrayToString(Array a)
        {
            StringBuilder builder = new StringBuilder();

            for (int i = 0; i < a.Length; i++)
            {
                if (i > 0)
                    builder.Append(",");

                builder.Append(a.GetValue(i).ToString());
            }

            return builder.ToString();
        }
    }
}
